/**
 * Created by ZYY on 2016/12/7.
 */
var data=[
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"113.29776032, 25.51924935",message:"附加消息"},
    {id:1001,coordinate:"113.29776032, 25.51924935",message:"附加消息"},
    {id:1001,coordinate:"106.82690749, 28.77273891",message:"附加消息"},
    {id:1001,coordinate:"106.82690749, 28.77273891",message:"附加消息"},
    {id:1001,coordinate:"110.16677412, 20.84082833",message:"附加消息"},
    {id:1001,coordinate:"120.43121832, 27.17108269",message:"附加消息"},
    {id:1001,coordinate:"118.70117995, 24.85031054",message:"附加消息"},
    {id:1001,coordinate:"118.70117995, 24.85031054",message:"附加消息"},
    {id:1001,coordinate:"101.67569514, 22.47902628",message:"附加消息"},
    {id:1001,coordinate:"101.67569514, 22.47902628",message:"附加消息"},
    {id:1001,coordinate:"113.02427868, 29.77063003",message:"附加消息"},
    {id:1001,coordinate:"113.02427868, 29.77063003",message:"附加消息"},
    {id:1001,coordinate:"113.73410300, 29.59432100",message:"附加消息"},
    {id:1001,coordinate:"110.09233013, 29.79501288",message:"附加消息"},
    {id:1001,coordinate:"121.70238386, 29.97723935",message:"附加消息"},
    {id:1001,coordinate:"121.70238386, 29.97723935",message:"附加消息"},
    {id:1001,coordinate:"118.89830319, 27.46554153",message:"附加消息"},
    {id:1001,coordinate:"112.67760402, 29.59291143",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"118.89830319, 27.46554153",message:"附加消息"},
    {id:1001,coordinate:"114.98907379, 31.47163968",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"114.87756114, 32.97349918",message:"附加消息"},
    {id:1001,coordinate:"114.87756114, 32.97349918",message:"附加消息"},
    {id:1001,coordinate:"89.54669617, 35.85727018",message:"附加消息"},
    {id:1001,coordinate:"90.27748815, 38.23823486",message:"附加消息"},
    {id:1001,coordinate:"90.27748815, 38.23823486",message:"附加消息"},
    {id:1001,coordinate:"118.52190658, 34.71420656",message:"附加消息"},
    {id:1001,coordinate:"96.39753411, 38.28168800",message:"附加消息"},
    {id:1001,coordinate:"96.39753411, 38.28168800",message:"附加消息"}
];
var provinceData = [];  //创建省 json
$.each(data, function(index) {
    //第一步：先将经纬度坐标转化成城市：省+市（地级城市）
    var asList = data[index].coordinate.toString().split(",");  //分割经纬度
    //调用百度地图API方法
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(asList[0], asList[1]);
    var gc = new BMap.Geocoder();
    gc.getLocation(point, function (rs) {
        //获得返回json数据格式：省：province 市：city 县/区：district 镇/街道：street 村/街道号streetNumber
        var addComp = rs.addressComponents;
        var province = {};
        province = addComp.province;
        //地图需要：只提供省名称
        if(addComp.province.substring(0,2) == "黑龙" || addComp.province.substring(0,2) == "内蒙"){
            province = addComp.province.substring(0,3);
        }else {
            province = addComp.province.substring(0,2);
        }
        provinceData.push(province);//将省份加入json数组中
    });
});
//定义两个json数据，分别用来存储 各个省的名称和其对应的机器人的数量
var dataP = [];
var dataN = [];
var allNums = 0;
var dataPN =[
    {name:"广东",value:0},{name:"青海",value:0},{name:"四川",value:0},{name:"海南",value:0},{name:"陕西",value:0},
    {name:"甘肃",value:0},{name:"云南",value:0},{name:"湖南",value:0},{name:"湖北",value:0},{name:"黑龙江",value:0},
    {name:"贵州",value:0},{name:"山东",value:0},{name:"江西",value:0},{name:"河南",value:0},{name:"河北",value:0},
    {name:"山西",value:0},{name:"安徽",value:0},{name:"福建",value:0},{name:"浙江",value:0},{name:"江苏",value:0},
    {name:"吉林",value:0},{name:"辽宁",value:0},{name:"台湾",value:0},{name:"新疆",value:0},{name:"广西",value:0},
    {name:"宁夏",value:0},{name:"内蒙古",value:0},{name:"西藏",value:0},{name:"北京",value:0},{name:"天津",value:0},
    {name:"上海",value:0},{name:"重庆",value:0},{name:"香港",value:0},{name:"澳门",value:0}
];
//接下来就烦了…… ~-~
setTimeout(function () {
    var i,m;
    for( i = 0;i < provinceData.length;i++){
        for( m = 0 ; m < dataPN.length;m++ ){
            if(provinceData[i].toString() == dataPN[m].name.toString()){
                dataPN[m].value +=1;
                allNums += dataPN[m].value;
             }
        }

    }
    //排序,根据数量从大到小排列各省
    function sortId(a,b){
        return a.value-b.value;
    }
    var newData = dataPN;
    newData.sort(sortId);
    var n;
    for( n = 0 ; n < newData.length;n++ ){
        dataP[n] = newData[n].name;
        dataN[n] = newData[n].value;
    }


    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '全国各省销量  (总量：'+allNums+'台)'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            data: ['机器人数量']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'value',

            boundaryGap: [0, 0.01]
        },
        yAxis: {
            type: 'category',
            axisLabel:{interval: 0},
            data: dataP
        },
        series: [
            {
                name: '机器人数量',
                type: 'bar',
                data: dataN
            }
        ]
    };
// 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
},2000);

